package org.design.factoryPattern;

import org.junit.Test;

/**
 * FactoryPattern
 * 工厂模式（Factory Pattern）是 Java 中最常用的设计模式之一，
 * 它提供了一种创建对象的方式，使得创建对象的过程与使用对象的过程分离。
 * 工厂模式提供了一种创建对象的方式，而无需指定要创建的具体类。
 * 通过使用工厂模式，可以将对象的创建逻辑封装在一个工厂类中，
 * 而不是在客户端代码中直接实例化对象，这样可以提高代码的可维护性和可扩展性。
 *
 * @author 樊温军
 * @date 2025/6/11 上午9:42
 */
public class FactoryPattern {

    public enum ShapeType {
        RECTANGLE, SQUARE, TRIANGLE;
    }

    public interface Shape {
        void draw();
    }

    public class Rectangle implements Shape {
        @Override
        public void draw() {
            System.out.println("Rectangle");
        }
    }

    public class Square implements Shape {
        @Override
        public void draw() {
            System.out.println("Square");
        }
    }

    public class Triangle implements Shape {
        @Override
        public void draw() {
            System.out.println("Triangle");
        }
    }

    public class ShapeFactory {
        public Shape getShape(ShapeType shapeType) {
            if (shapeType == null) {
                return null;
            }
            if (shapeType.equals(ShapeType.RECTANGLE)) {
                return new Rectangle();
            } else if (shapeType.equals(ShapeType.SQUARE)) {
                return new Square();
            } else if (shapeType.equals(ShapeType.TRIANGLE)) {
                return new Triangle();
            }
            return null;
        }
    }

    @Test
    public void test() {
        FactoryPattern factoryPattern = new FactoryPattern();
        ShapeFactory shapeFactory = factoryPattern.new ShapeFactory();
        Shape shape = shapeFactory.getShape(ShapeType.RECTANGLE);
        shape.draw();
        shape = shapeFactory.getShape(ShapeType.SQUARE);
        shape.draw();
        shape = shapeFactory.getShape(ShapeType.TRIANGLE);
        shape.draw();
    }
}
